/*
 * Copyright (c) 2025 Huawei Device, Inc. Ltd. and <马弓手>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "NavigationBar.h"
#include "CordovaViewController.h"
REGISTER_PLUGIN_CLASS(NavigationBar)

bool NavigationBar::execute(const string&  action, cJSON* args, CallbackContext cbc) 
{
    if(action == "onArKTsResult") {
        return onArKTsResult(args);
    }
    
    if(action == "_ready") {
        m_cbc = cbc;
        executeArkTs("/pages/NavigationBar_ready",0, "","NavigationBar", m_cbc);
    }
    
    if(action == "show") {
        executeArkTs("/pages/NavigationBar_show",1, "","NavigationBar", m_cbc);
    }
    
    if(action == "hide") {
        executeArkTs("/pages/NavigationBar_show",0, "","NavigationBar", m_cbc);
    }
    
    if(action == "backgroundColorByHexString") {
        string strNavigationBarBackgroundColor = m_preferences->getString("NavigationBarBackgroundColor", "#ffffff");
         CordovaArgs cordovaArgs(args);
        int nArraySize = cordovaArgs.getSize();
        if(nArraySize > 0) {
            strNavigationBarBackgroundColor = cordovaArgs.getString(0);
            executeArkTs("/pages/NavigationBar_bgColor",0, strNavigationBarBackgroundColor.c_str(),"NavigationBar", m_cbc);
        }
    }
    return true;
}

void NavigationBar::initialize()
{
    string strNavigationBarBackgroundColor = m_preferences->getString("NavigationBarBackgroundColor", "#ffffff");
    string strNavigationBarFontColor = m_preferences->getString("NavigationBarFontColor", "#000000");
    string strNavigationBarFontAlign = m_preferences->getString("NavigationBarFontAlign", "center");
    bool isNavigationBarLight = m_preferences->getBoolean("NavigationBarLight", true);
    
    cJSON* pJson = cJSON_CreateObject();
    cJSON_AddStringToObject(pJson, "NavigationBarBackgroundColor", strNavigationBarBackgroundColor.c_str());
    cJSON_AddStringToObject(pJson, "NavigationBarFontColor", strNavigationBarFontColor.c_str());
    cJSON_AddStringToObject(pJson, "NavigationBarFontAlign", strNavigationBarFontAlign.c_str());
    if(isNavigationBarLight) {
        cJSON_AddTrueToObject(pJson, "NavigationBarLight");
    } else {
        cJSON_AddFalseToObject(pJson, "NavigationBarLight");
    }
    const char* pArgs = cJSON_Print(pJson);
    executeArkTs("/pages/NavigationBar_initialize",0, pArgs,"NavigationBar", m_cbc);
    cJSON_Delete(pJson);
    free((void*)pArgs);
}

bool NavigationBar::onArKTsResult(cJSON* args)
{
    string content = cJSON_GetObjectItem(args, "content")->valuestring;
    cJSON* json = cJSON_GetObjectItem(args, "result");
    if(content == "NavigationBar_ready") {
        if(json != NULL && json->type == cJSON_Array) {
            int count = cJSON_GetArraySize(json);
            if(count > 0) {
                string result = cJSON_GetArrayItem(json, 0)->valuestring;
                bool isFullScreen = false;
                if(result == "true")
                    isFullScreen = true;
                PluginResult pluginResult(PluginResult::Status::OK, isFullScreen);
                m_cbc.sendPluginResult(pluginResult);
            } else {
                m_cbc.error("please manually open it in the notification");
            }
        }
    }
    return true;
}